package com.example.im.ui.activity

import android.content.Intent
import android.graphics.Bitmap
import android.os.Environment
import com.cjt2325.cameralibrary.JCameraView
import com.cjt2325.cameralibrary.listener.ErrorListener
import com.cjt2325.cameralibrary.listener.JCameraListener
import com.example.im.R
import com.example.im.utils.LogUtils
import com.example.im.mvp.base.BasePresenter
import com.example.im.ui.abstracts.AbstractBaseActivity
import kotlinx.android.synthetic.main.activity_camera.*
import java.io.File
import android.os.Build
import android.view.View
import com.example.im.common.Constants


class CameraActivity : AbstractBaseActivity<BasePresenter<*>>(), ErrorListener, JCameraListener {

    override val layoutId: Int
        get() = R.layout.activity_camera

    override fun initView() {
        if (Build.VERSION.SDK_INT >= 19) {
            val decorView = window.decorView
            decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        } else {
            val decorView = window.decorView
            val option = View.SYSTEM_UI_FLAG_FULLSCREEN
            decorView.systemUiVisibility = option
        }
        camera_view.setSaveVideoPath(Environment.getExternalStorageDirectory().path + File.separator + "JCamera")
        camera_view.setFeatures(JCameraView.BUTTON_STATE_ONLY_RECORDER)
        camera_view.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE)
        camera_view.setErrorLisenter(this)
        camera_view.setJCameraLisenter(this)
        camera_view.setLeftClickListener {
            finish()
        }
    }

    override fun initPresenter() {

    }

    override fun onResume() {
        super.onResume()
        camera_view.onResume()
    }

    override fun onPause() {
        super.onPause()
        camera_view.onPause()
    }

    override fun AudioPermissionError() {
        LogUtils.e("没有获取权限回调")
    }

    override fun onError() {
        LogUtils.e("打开Camera失败回调")
    }

    override fun recordSuccess(url: String?, firstFrame: Bitmap?) {
        LogUtils.e("获取视频路径$url")
        val intent = Intent()
            .putStringArrayListExtra(Constants.BUNDLE_VIDEO_PATH, arrayListOf(url))
        setResult(RESULT_OK, intent)
        finish()
    }

    override fun captureSuccess(bitmap: Bitmap?) {
        bitmap!!.density
        LogUtils.e("获取图片bitmap")
        finish()
    }


}